package com.cos.lession72.slice;

import com.cos.lession72.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

public class TextFieldAbilitySlice extends AbilitySlice {
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_textfield);

        TextField tf1 = (TextField) findComponentById(ResourceTable.Id_tf1);
        //js 聚焦失焦事件的集合
        tf1.setFocusChangedListener(new Component.FocusChangedListener() {
            @Override
            public void onFocusChange(Component component, boolean b) {
                // boolean 有焦点的时候为true
                    if (b) {
                        //聚焦
//                        tf1.setText("haha");
                    }else{
                        //失焦
                        //取输入框文本值
                        String value = tf1.getText();
                        if (value.trim().length()<6) {
                            // 设置背景
//                            ShapeElement element = new ShapeElement();
//                            element.setRgbColor(new RgbColor(255,0,0));
//                            tf1.setBackground(element);
                            Text t = (Text) findComponentById(ResourceTable.Id_tf1msg);
                            //设置字体颜色
                            t.setTextColor(Color.RED);
                            t.setText("用户名不能少于6位");

                        }
//                        tf1.setText("hehe");
//                        //设置输入框不可用
//                        tf1.setEnabled(false);
                }
            }
        });


    }
}
